import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { routes } from '@/router'

export const usePermissionStore = defineStore('permission', () => {
  // 路由列表
  const routesList = ref([])
  
  /**
   * @description 生成路由
   * @param {Array} asyncRoutes - 异步路由
   * @returns {Array} 生成的路由
   */
  const generateRoutes = (asyncRoutes = []) => {
    // 这里可以根据用户角色过滤路由
    // 示例中我们直接返回所有路由
    routesList.value = routes.filter(route => !route.meta?.hidden)
    return routesList.value
  }
  
  /**
   * @description 获取菜单
   */
  const menuList = computed(() => {
    // 过滤掉不需要显示在菜单中的路由
    const filterHiddenRoute = (routes) => {
      return routes.filter(route => {
        if (route.meta?.hidden) {
          return false
        }
        
        if (route.children && route.children.length) {
          route.children = filterHiddenRoute(route.children)
        }
        
        return true
      })
    }
    
    return filterHiddenRoute(routesList.value)
  })
  
  return {
    routesList,
    menuList,
    generateRoutes
  }
}) 